package com.cht.commonUtil.requirements;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cht.commonUtil.structure.I1S2;
import com.cht.commonUtil.webInit.SP;
import com.cht.commonUtil.widgets.Widgets;
import com.google.gson.Gson;

public class GetBindingReqirement extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		List<I1S2> reqs = new ArrayList<I1S2>();
		
		response.setCharacterEncoding(SP.UIcodeSet);
		response.setContentType( "application/json" );
		
		if( SP.isBindingRequirementOnDeliver ) {
			String sql = "SELECT t2.reqId,t2.reqSID,t2.reqDesc FROM mpchkinreq t1,reqs t2,chkinmain t3 WHERE t1.chkinmainid=t3.chkinmainid AND t3.exeVer=? AND t1.reqId=t2.reqId";
			int exeVer = Integer.parseInt( request.getParameter("exeVer") );
			
			Connection conn = null;
			PreparedStatement ps = null;
			ResultSet rs = null;
			
			try {
				conn = Widgets.getConn();
				ps = conn.prepareStatement(sql);
				ps.setInt(1, exeVer);
				rs = ps.executeQuery();
				while( rs.next() ) {
					I1S2 req = new I1S2();
					req.setI1( rs.getInt("reqId") );
					req.setS1( rs.getString("reqSID") );
					req.setS2( rs.getString("reqDesc") );
					reqs.add(req);
				}
			} catch (Exception e) {
				Widgets.errLog(e);
			} finally {
				try {
					if ( rs !=null ) {rs.close();}
					if ( ps !=null ) {ps.close();}
					if ( conn !=null ) {conn.close();}	
				} catch(Exception e) { Widgets.errLog(e); } 
				
			} 
		}
		
		response.getWriter().write( new Gson().toJson(reqs) );
		
	}

}
